package com.richapp.launcher.database.infrared.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.richapp.launcher.database.infrared.InfraredDatabaseHelper;
import com.richapp.launcher.database.infrared.mode.BrandModelButton;

import java.util.List;

public class BrandModelButtonDao {

	private Context context;
	private InfraredDatabaseHelper helper = null;
	private Dao<BrandModelButton,Integer> dao = null;

	public BrandModelButtonDao(Context context) {
		this.context = context;
		try {
			helper = InfraredDatabaseHelper.getHelper(this.context);
            dao = helper.getDao(BrandModelButton.class);
            dao.setObjectCache(false);
		} catch (Exception e) {
			Log.e("error", e.getMessage());
		}
	}

    public void add(BrandModelButton brandModelButton){
        try {
            dao.createOrUpdate(brandModelButton);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public void deleteAll(){
        try {
            DeleteBuilder deleteBuilder = dao.deleteBuilder();
            deleteBuilder.delete();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public BrandModelButton get(int brandModelId,int buttonId){
        BrandModelButton brandModelButton = null;
        try{
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("brandModelId",brandModelId).and().eq("buttonId",buttonId);
            Object obj = queryBuilder.queryForFirst();
            if(obj instanceof BrandModelButton){
                brandModelButton = (BrandModelButton)obj;
            }
        }catch (Exception e){
            Log.e("error", e.getMessage());
        }
        return brandModelButton;
    }

    public List<BrandModelButton> getList(int brandModelId){
        List<BrandModelButton> brandModelButtonList = null;
        try{
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.where().eq("brandModelId",brandModelId);
            queryBuilder.orderBy("buttonId",true);
            brandModelButtonList = queryBuilder.query();
        }catch (Exception e){
            Log.e("error", e.getMessage());
        }
        return brandModelButtonList;
    }

}
